local skel = fk.CreateSkill {
  name = "rmt__baijiangdengwang",
}

---@param player  ServerPlayer
local function baijiang(self, event, target, player, data)
  local room = player.room
  local record = room:getTag("rmt__baijiangdengwang_record")
  if record == nil then
    record = {generals = {}, kingdoms = {}}
    for packageName, package in pairs(Fk.packages) do
      if package.extensionName == "aaa_Romantic" then
        for _, general in ipairs(package.generals) do
          table.insertIfNeed(record.kingdoms, general.kingdom)
          if Fk:canUseGeneral(general.name) then
            table.insert(record.generals, general.name)
          end
        end
      end
    end
    room:setTag("rmt__baijiangdengwang_record", record)
  end
  local generals = table.simpleClone(record.generals)

  if #generals == 0 then return end
  local existingGenerals = {"rmt__jisi"}
  for _, p in ipairs(room.players) do
    table.insert(existingGenerals, p.general)
    table.insert(existingGenerals, p.deputyGeneral)
  end
  generals = table.filter(generals, function (name) return not table.contains(existingGenerals, name) end)
  if #generals == 0 then return end
  local rndGenerals = table.random(generals, 3)

  -- 为了更快达成变更势力，让未选择的势力有概率优先出现（可调整概率）
  local kingdom_used = player:getTableMark(skel.name.."_kingdom")
  if #generals > 3 and #kingdom_used < #record.kingdoms then
    local rate = 0.6
    if math.random() < rate then
      local extra = table.filter(generals, function (name)
        local g = Fk.generals[name]
        return not table.contains(rndGenerals, name) and not table.contains(kingdom_used, g.kingdom)
      end)
      if #extra > 0 then
        rndGenerals[math.random(1, 3)] = table.random(extra)
      end
    end
  end

  local general = room:askToChooseGeneral(player, {generals = rndGenerals, n = 1, no_convert = true})---@type string
  room:addTableMarkIfNeed(player, skel.name.."_kingdom", Fk.generals[general].kingdom)
  room:changeHero(player, general, false, true, true, false, false)
end

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return target == player
    end
  end,
  on_use = baijiang,
})

skel:addEffect(fk.GameStart, {
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return true
    end
  end,
  on_use = baijiang,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player == target and player.phase == Player.Start then
      local tag = player.room:getTag("rmt__baijiangdengwang_record")
      if tag and tag.kingdoms then
        return #player:getTableMark(skel.name.."_kingdom") >= #tag.kingdoms
      end
    end
  end,
  on_use = baijiang,
})

Fk:loadTranslationTable{
  ["rmt__baijiangdengwang"] = "拜将登王",
  [":rmt__baijiangdengwang"] = "游戏开始时，或受到伤害后，你可以从“浪漫”包中变更一次副将，所有势力的武将均变更过后，你准备阶段也可变更！",
}

return skel
